package com.ttdev.mmms.controller.member;

import com.ttdev.mmms.entity.MemberInfo;

import com.ttdev.mmms.service.MemberService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Coca-Cola
 * @sketch 会员信息维护管理控制器
 * @date 2019/12/2 9:27
 */
@Controller
@RequestMapping("/memberInFo")
public class MemberController {

    @Autowired
    private MemberService memberService ;

    //查询
    @RequestMapping("/select")
    public ModelAndView findAll(@RequestParam("storeId") Integer storeId){

        List<MemberInfo> list = memberService.findsAll(storeId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("member/selectMemberInFo");
        modelAndView.addObject("memberInFoSelect",list);
        return modelAndView;
    }

    //注册
    @RequestMapping("/addMember")
    public String add(MemberInfo member) {

        int i = memberService.add(member);
//        session.getAttribute("storeManage");

        return "redirect:/storeIndex";
    }

    //根据id查询会员信息
    @RequestMapping("/update/{cardId}")
    public ModelAndView updateId(@PathVariable("cardId") int cardId){
        List<MemberInfo> list = memberService.queryMember(cardId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("member/updateMember");
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    //修改信息
    @RequestMapping("/upd")
    public String updateId(MemberInfo member){
        int i = memberService.update(member);
        return "redirect:/storeIndex";
    }

    //删除信息
    @RequestMapping("/delete/{cardId}")
    public String delete (@PathVariable("cardId") Integer cardId){
        int i = memberService.delete(cardId);
        return "redirect:/storeIndex";
    }

    // 查询用户名是否存在
    @RequestMapping("/selectMemberName")
    public void selectStoreName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uname;
        String user = request.getParameter("user");
//        System.out.println(storeName);
        Integer i = memberService.selectMemberName(user);

        if (i == null){
            i = 0;
        }

        // 如果不为空则用户名已存在
        if (i == 1001 || i > 1001){
            uname = "用户名已存在";
        } else {
            uname = "可以使用此用户名";
        }

        JSONObject json = new JSONObject();
        json.put("uname",uname);
//            System.out.println(i);
        // json乱码解决
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(json.toString());
    }

}
